﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.utils.*;
    import plugin.*;

    public class ItemImageAsset extends EventDispatcher {

        public static const TYPE_UNKNOWN:int = 0;
        public static const TYPE_BITMAPDATA:int = 1;
        public static const TYPE_MOVIECLIP:int = 2;

        public static var s_count:int = 0;
        public static var s_pixels:int = 0;
        private static var m_loadedAssets:Dictionary = new Dictionary();

        public var bitmapData:BitmapData;
        public var contentClass:Class;
        public var content:DisplayObject;
        public var symbolName:String;
        public var name:String;
        public var type:int = 0;
        private var m_loaded:Boolean;
        private var m_loadStarted:Boolean;
        private var m_asset:Object;
        private var m_url:String;

        public function ItemImageAsset(_arg1:XML):void{
            var _local3:String;
            super();
            if (_arg1.@url != ""){
                this.m_url = Global.getAssetURL(_arg1.@url);
            };
            this.name = String(_arg1.@name);
            var _local2:String = String(_arg1.@loadClass);
            if (_local2.length > 0){
                this.type = TYPE_BITMAPDATA;
                if (_local2 == "mc"){
                    this.type = TYPE_MOVIECLIP;
                };
                _local3 = String(_arg1.@itemName);
                this.symbolName = ((this.name + "_") + _local3);
            };
        }
        public static function injectAsset(_arg1:String, _arg2:DisplayObject):void{
            var _local3:String = GlobalEngine.getFlashVar("asset_url").toString();
            var _local4:String = (_local3 + _arg1);
            var _local5:Object = m_loadedAssets[_local4];
            if (_local5 == null){
                m_loadedAssets[_local4] = {
                    loaded:true,
                    loader:null,
                    url:_local4,
                    content:_arg2
                };
                trackAsset(_arg1, _arg2);
            };
        }
        public static function injectAssets(_arg1):void{
            var _local3:String;
            var _local4:Object;
            var _local5:String;
            var _local6:Object;
            if (!_arg1.hasOwnProperty("assets")){
                return;
            };
            var _local2:Dictionary = (_arg1["assets"] as Dictionary);
            if (_local2 != null){
                _local3 = GlobalEngine.getFlashVar("asset_url").toString();
                for (_local4 in _local2) {
                    _local5 = (_local3 + _local4);
                    _local6 = m_loadedAssets[_local5];
                    if (_local6 == null){
                        m_loadedAssets[_local5] = {
                            loaded:true,
                            loader:null,
                            url:_local5,
                            content:_local2[_local4]
                        };
                        trackAsset((_local4 as String), _local2[_local4]);
                    };
                };
                _arg1["assets"] = null;
            };
        }
        public static function trackAsset(_arg1:String, _arg2:Object):void{
            var _local3:Bitmap = (_arg2 as Bitmap);
            var _local4:ConsoleStub = ConsoleStub.getInstance();
            if (_local3 == null){
                return;
            };
            s_count++;
            s_pixels = (s_pixels + (_local3.width * _local3.height));
            if (_local4 != null){
                _local4.print("Asset", (((("Locator = " + _arg1) + ", Pixels = ") + int((((_local3.width * _local3.height) * 4) / 0x0400))) + "KB"));
            };
        }
        public static function printStats():void{
            var _local1:ConsoleStub = ConsoleStub.getInstance();
            if (_local1 != null){
                _local1.print("Asset", (((("[ImageAsset] Bitmap = " + s_count) + ", Pixels = ") + int(((s_pixels * 4) / 0x0400))) + "KB"), 3);
            };
        }

        public function get locator():String{
            var _local1:int = this.m_url.indexOf("/assets/");
            if (_local1 > 0){
                return (this.m_url.substring((_local1 + 8)));
            };
            return (this.m_url);
        }
        public function load():void{
            var _local1:Loader;
            if ((((this.m_loaded == false)) || ((this.m_loadStarted == false)))){
                this.m_asset = this.getAssetByUrl(this.m_url);
                if (this.m_asset == null){
                    _local1 = LoadingManager.load(this.m_url, this.onImageLoaded);
                    this.m_asset = {
                        loaded:false,
                        loader:_local1,
                        url:this.m_url,
                        content:null
                    };
                    m_loadedAssets[this.m_url] = this.m_asset;
                } else {
                    if (this.m_asset.loader){
                        this.m_asset.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onImageLoaded);
                    } else {
                        this.handleContent();
                    };
                };
                this.m_loadStarted = true;
            } else {
                if (this.m_loaded){
                    dispatchEvent(new LoaderEvent(LoaderEvent.LOADED));
                };
            };
        }
        public function isLoaded():Boolean{
            return (this.m_loaded);
        }
        protected function loadImage(_arg1:String):void{
        }
        protected function getAssetByUrl(_arg1:String):Object{
            return (m_loadedAssets[_arg1]);
        }
        public function replicate(_arg1:Number):AnimatedAsset{
            var _local2:AnimatedAsset = (this.m_asset.content as AnimatedAsset);
            if (_local2 != null){
                this.content = _local2;
                this.bitmapData = null;
                return (_local2.duplicate(_arg1));
            };
            return (null);
        }
        public function cache(_arg1:AnimatedAsset):void{
            this.m_asset.content = _arg1;
            this.bitmapData = null;
            this.content = _arg1;
        }
        protected function onImageLoaded(_arg1:Event):void{
            var _local2:LoaderInfo = (_arg1.currentTarget as LoaderInfo);
            var _local3:Loader = _local2.loader;
            _local3.removeEventListener(Event.COMPLETE, this.onImageLoaded);
            if (this.m_asset){
                if (this.m_asset.loader != null){
                    trackAsset(this.locator, _local3.content);
                };
                this.m_asset.loader = null;
                this.m_asset.content = _local3.content;
                this.handleContent();
            };
        }
        protected function handleContent():void{
            var _local2:MovieClip;
            var _local3:Class;
            var _local4:Object;
            this.content = (this.m_asset.content as DisplayObject);
            var _local1:int;
            if ((this.content is Bitmap)){
                this.bitmapData = (this.content as Bitmap).bitmapData;
                this.type = TYPE_BITMAPDATA;
            } else {
                if ((this.content is MovieClip)){
                    _local2 = (this.content as MovieClip);
                    if (this.symbolName != null){
                        if (_local2.loaderInfo.applicationDomain.hasDefinition(this.symbolName)){
                            _local3 = (_local2.loaderInfo.applicationDomain.getDefinition(this.symbolName) as Class);
                            if (_local3){
                                if (this.type == TYPE_MOVIECLIP){
                                    this.contentClass = _local3;
                                } else {
                                    this.bitmapData = new _local3(0, 0);
                                    if ((((((this.bitmapData == null)) || ((this.bitmapData.width == 0)))) || ((this.bitmapData.height == 0)))){
                                        ErrorManager.addError(((("Invalid bitmap data for class " + this.symbolName) + " in ") + this.m_url));
                                    };
                                };
                            };
                        } else {
                            ErrorManager.addError(((("Symbol " + this.symbolName) + " not found in ") + this.m_url));
                        };
                    };
                };
            };
            this.m_loaded = true;
            dispatchEvent(new LoaderEvent(LoaderEvent.LOADED));
        }

    }
}//package Classes 
